package ui;

import java.util.HashMap;
import java.util.Map;

import utils.ImageDownloader;
import android.util.Log;
import android.widget.ImageView;
import cfg.Flickr;

import com.webservice.IWebServiceTask;
import com.webservice.WebServiceEvent;
import com.webservice.WebServicePhotosGetSizes;
import com.webservice.objects.FlickrPhoto;
import com.webservice.objects.FlickrPhotoSizes;

public class Thumbnail implements IWebServiceTask {

	private FlickrPhoto photo;
	private ImageView thumbnailView;

	public Thumbnail(ImageView p_thumbnailView, FlickrPhoto p_photo) {
		thumbnailView = p_thumbnailView;
		photo = p_photo;
	}

	public void request() {

		Log.d("Thumbnail REQUEST", photo.toString());
		// Instantiate the Web Service Class with he URL of the web service not
		// that you must pass

		Map<String, String> params = new HashMap<String, String>();
		params.put("method", "flickr.photos.getSizes");
		params.put("photo_id", photo.id);
		params.put("format", "json");
		params.put("nojsoncallback", "1");
		params.put("api_key", Flickr.API_KEY);

		WebServicePhotosGetSizes ws_recent = new WebServicePhotosGetSizes(this,
				params);
		ws_recent.execute();
	}

	public void draw() {
		Log.d("Thumbnail.", "draw");
		ImageDownloader imageManager = new ImageDownloader();
		imageManager.download(photo.getThumbnail(), thumbnailView);
		photo.thumb_loaded = true;
	}

	public void onTaskCompleted(WebServiceEvent e) {
		Log.d("Thumbnail onTaskCompleted", e.response.toString());
		if (e.EVENT_ID == "WebServicePhotosGetSizes") {
			FlickrPhotoSizes o = (FlickrPhotoSizes) e.response;
			photo.sizes = o;

			draw();
		}
	}
	
	@Override
	public String toString() {
		return (" Thumbnail for : "+photo);
	}
}
